#include "yocto/utest/run.hpp"
#include "kernel/solution.hpp"

#include "yocto/ios/ocstream.hpp"
#include "yocto/lua/lua-state.hpp"
#include "yocto/lua/lua-config.hpp"
#include "yocto/code/rand.hpp"

using namespace yocto;


YOCTO_UNIT_TEST_IMPL(chemsys)
{
	_rand.wseed();
	
	if( argc <= 1 )
		throw exception("%s: need script.lua", argv[0] );
	Library    lib;
	ChemSys    chs(lib);
	Lua::State VM;
	lua_State *L = VM();
	Lua::Config::DoFile( L, argv[1] );
	lib.load(L,"library");
	chs.load(L,"chemsys");
	
	chs.compile();
	
	chs.randomX();
	chs.computeGammaAndPhi(0,true);
	chs.computeXi();
	chs.computeChi();
	
	std::cerr << "-- out of equilibrium: " << std::endl;
	std::cerr << "X="     << chs._X()     << std::endl;
	std::cerr << "Gamma=" << chs._Gamma() << std::endl;
	std::cerr << "Phi="   << chs._Phi()   << std::endl;
	
	std::cerr << "Solving random case:" << std::endl;
	chs.solveGamma(0);
	chs.computeGammaAndPhi(0,true);
	chs.computeXi();
	chs.computeChi();	
	std::cerr << "-- at equilibrium: " << std::endl;
	std::cerr << "X="     << chs._X()     << std::endl;
	std::cerr << "Gamma=" << chs._Gamma() << std::endl;
	std::cerr << "Phi="   << chs._Phi()   << std::endl;
	std::cerr << "Xi="    << chs._Xi()    << std::endl;
	std::cerr << "dtGam=" << chs._dtGam() << std::endl;
	
	
	lib.close();
	Solution S(lib);
	Solution dS(lib);
	
	S.load_from(chs);
	dS.zero();
	std::cerr << "S="; S.display(std::cerr);
	
	chs.updateRate(dS, 0.0, S);
	std::cerr << "dS="; dS.display(std::cerr);
	
	
}
YOCTO_UNIT_TEST_DONE()
